util={};
util.getHeader=function() {
    return {
        "Content-Type": "application/json",
        "authorize": "",
        "timestamp": (new Date()).valueOf()
    };
};
/**
 * 请求工具方法
 * @param requestType
 * @param url
 * @param requestData
 * @param successFunction
 */
util.request=function(requestType,url,requestData,successFunction){
    // Layer.loading();
    $.ajax({
        type:requestType,
        url:url,
        dataType:'json',
        headers:util.getHeader(),
        data:JSON.stringify(requestData),
        success:function(response) {
            if (response.code == -1) {
                // Layer.message('系统异常，请联系管理员','2');
            }else if (response.code == -2) {
                // Layer.message('未登录','5');
            }
            else{
                successFunction(response);
            }
            // layer.closeAll('loading');
        },
        error:function(error){
            // layer.closeAll('loading');
             Msg.errors('服务器异常，请联系管理员')
        }
    });
};
util.requestParams=function(requestType,url,requestData,successFunction){
    // Layer.loading();
    $.ajax({
        type:requestType,
        url:url,
        dataType:'json',
        headers:util.getHeader(),
        data:requestData,
        success:function(response) {
            if (response.code == -1) {
                // Layer.message('系统异常，请联系管理员','2');
            }else if (response.code == -2) {
                // Layer.message('未登录','5');
            }
            else{
                successFunction(response);
            }
            // layer.closeAll('loading');
        },
        error:function(error){
            // layer.closeAll('loading');
            Msg.errors('服务器异常，请联系管理员')
        }
    });

}

/**
 * 部门数据初始化create by zyou
 * selectId : 下拉选框的id
 * organId : 已选的部门Id
 */
function ajaxGetDept(selectId,organId) {
    $.ajax({
        url: sysInfo.basePath + "/org/get_org_tree.do",
        type: "POST",
        dataType: "json",
        async:false,
        success: function (obj) {
            var depts = obj.data;
            var html = "";
            depts.forEach(function (e) {
                if(organId != undefined && organId != null  && organId.trim().length >0 && organId == e.id){
                    html += "<option value='"+e.id+"' selected>"+e.name+"</option>"
                }else{
                    html += "<option value='"+e.id+"'>"+e.name+"</option>"
                }

            });
            $("#"+selectId).empty().append(html);
        }
    });
}

/**
 * 验证是否为空
 */
function checkEmpty(val){
    var check = false;
    if(undefined == val || val == null || val.trim().length == 0 ){
        return false;
    }else{
        check = true;
    }
    return check;
}

/**
 * 验证文本框是否为空并提示信息
 * @param obj 对象
 * @param msg 提示信息
 * @returns {boolean}
 */
function verifyEmpty(obj,msg){
    var check = false;
    var val = obj.val();
    if(undefined == val || val == null || val.trim().length == 0 ){
        layer.tips(msg, obj, {
            tips: [1, 'red'],
            tipsMore: true
        });
        return false;
    }else{
        check = true;
    }
    return check;
}
/**
 * 验证文本框输入的长度并提示信息
 * @param obj 对象
 * @param len 限制的长度
 * @param msg 提示信息
 * @returns {boolean}
 */
function verifyLength(obj,len,msg){
	var check = false;
	var val = obj.val();
	if(val != undefined && val != null && val.length <= len){
		check = true;
	}else{
		layer.tips(msg, obj, {
            tips: [1, 'red'],
            tipsMore: true
        });
	}
	return check;
}
//==========================================================
//--------------去掉左右空格-----------------------------------
String.prototype.trim = function () {
    return this.replace(/(^\s*)|(\s*$)/g, "");
};
//--------------去掉左空格-------------------------------------
String.prototype.lTrim = function () {
    return this.replace(/(^\s*)/g, "");
};
//--------------去掉右空格-------------------------------------
String.prototype.rTrim = function () {
    return this.replace(/(\s*$)/g, "");
};
//--------------去掉html的空格符------------------------------
String.prototype.hTrim = function () {
    return this.replace(/(\&nbsp\;)/g, "");
};
String.prototype.verifyEmpty = function () {
    if (undefined != this && this.trim().length > 0) {
        return false;
    }
    return true;
}
